﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;
using WalkingTec.Mvvm.Mvc;
using TyMES.Model;
using TyMES.Model.BaseInformation;
using TyMES.TemporaryClass;
using TyMES.ViewModel.BaseInformation.RouteVMs;
using System.Threading.Tasks;


namespace TyMES.BaseInformation.Controllers
{
    [AuthorizeJwtWithCookie]
    public partial class RouteController : BaseApiController
    {
        private StationServices _stationServices;

        public RouteController(StationServices stationServices)
        {
            this._stationServices = stationServices;
        }

        [ActionDescription("Sys.Search")]
        [HttpPost("[action]")]
        public IActionResult SearchRoute(TyMES.ViewModel.BaseInformation.RouteVMs.RouteSearcher searcher)
        {
            if (ModelState.IsValid)
            {
                var vm = Wtm.CreateVM<TyMES.ViewModel.BaseInformation.RouteVMs.RouteListVM>();
                vm.Searcher = searcher;
                return Content(vm.GetJson());
            }
            else
            {
                return BadRequest(ModelState.GetErrorJson());
            }
        }

        [ActionDescription("Sys.Export")]
        [HttpPost("[action]")]
        public IActionResult RouteExportExcel(TyMES.ViewModel.BaseInformation.RouteVMs.RouteSearcher searcher)
        {
            var vm = Wtm.CreateVM<TyMES.ViewModel.BaseInformation.RouteVMs.RouteListVM>();
            vm.Searcher = searcher;
            vm.SearcherMode = ListVMSearchModeEnum.Export;
            return vm.GetExportData();
        }

        [ActionDescription("Sys.CheckExport")]
        [HttpPost("[action]")]
        public IActionResult RouteExportExcelByIds(string[] ids)
        {
            var vm = Wtm.CreateVM<TyMES.ViewModel.BaseInformation.RouteVMs.RouteListVM>();
            if (ids != null && ids.Count() > 0)
            {
                vm.Ids = new List<string>(ids);
                vm.SearcherMode = ListVMSearchModeEnum.CheckExport;
            }
            return vm.GetExportData();
        }

        [ActionDescription("Sys.CheckExport")]
        [HttpPost("Sync")]
        public async Task<IActionResult> SycnRouteandOperationAsync(Order_Model _order)
        {
            try
            {
                var route = await _stationServices.GetRoute(_order.MaterialCode, _order);
                return Ok(route.RouteName + "同步成功");
            }
            catch(Exception ex) 
            {
                return BadRequest(ex.ToString());
            }
        }


    }
}


